package com.hy.example.server.core.service;

import com.hy.example.common.api.result.ApiPageResult;
import com.hy.example.server.common.dto.ExampleDemoDTO;
import com.hy.example.server.common.param.ExampleDemoParam;
import com.hy.example.server.common.param.ExampleDemoQueryParam;
import com.hy.example.server.common.param.ExampleDemoUpdateParam;
import com.hy.example.server.common.vo.ExampleDemoItemVO;
import com.hy.example.server.core.dbaccess.ExampleDemoPO;
import com.hy.example.server.core.dbaccess.ExampleDemoRepository;
import com.hy.example.server.core.dbaccess.QExampleDemoPO;
import com.hy.example.shared.exception.BusinessException;
import com.hy.example.shared.utils.CommonUtils;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ExampleDemoService {

    private static final QExampleDemoPO qExampleDemoPO = QExampleDemoPO.exampleDemoPO;
    private static final OrderSpecifier<?> defaultOrder = qExampleDemoPO.id.desc();
    @Autowired
    private ExampleDemoRepository exampleDemoRepository;

    public ApiPageResult<ExampleDemoItemVO> page(ExampleDemoQueryParam queryParam, Integer pageNum, Integer pageSize) {
        return ApiPageResult.with(exampleDemoRepository.findAll(buildCondition(queryParam),
                        QPageRequest.of(pageNum - 1, pageSize, defaultOrder))
                .map(this::convertItemVO));

    }

    private ExampleDemoItemVO convertItemVO(ExampleDemoPO exampleDemoPO) {
        return ExampleDemoItemVO.builder()
                .id(exampleDemoPO.getId())
                .name(exampleDemoPO.getName())
                .build();
    }


    public ExampleDemoDTO detail(Long id) {
        ExampleDemoPO exampleDemoPO = exampleDemoRepository.findById(id)
                .orElseThrow(() -> new BusinessException("查询的数据不存在！"));
        return ExampleDemoDTO.builder()
                .id(exampleDemoPO.getId())
                .name(exampleDemoPO.getName())
                .build();
    }

    public void add(ExampleDemoParam param) {
        ExampleDemoPO exampleDemoPO = CommonUtils.copyProperties(param, ExampleDemoPO.class);
        exampleDemoRepository.save(exampleDemoPO);
    }

    public void update(ExampleDemoUpdateParam param) {
        ExampleDemoPO exampleDemoPO = exampleDemoRepository.findById(param.getId())
                .orElseThrow(() -> new BusinessException("修改的数据不存在！"));
        CommonUtils.copyProperties(param, exampleDemoPO);
        exampleDemoRepository.save(exampleDemoPO);
    }

    public void delete(Long id) {
        exampleDemoRepository.deleteById(id);
    }

    private BooleanExpression buildCondition(ExampleDemoQueryParam queryParam) {
        BooleanExpression condition = qExampleDemoPO.isNotNull();
        if (StringUtils.hasLength(queryParam.getName())) {
            condition = condition.and(qExampleDemoPO.name.eq(queryParam.getName()));
        }
        return condition;
    }
}
